
/* ###############################################

		1. 작    성   일 	>> 	2007. 1. 28
	
		2. 파    일   명 	>> 	PSocket.h

 		3. 내          용 	>> 	소켓객체...
 		
 		4. 작   성    자 	>>	최철호
 		
 		5. 최종수정일	>>	2007. 1. 28
										
################################################ */

#include "PSocket.h"


PSocket::PSocket( int domain, int type, int protocol )
{
	this->socket = ::socket( domain, type, protocol );
	memset( &(this->socketAddress), 0, sizeof(SockAddr) );
}

PSocket::PSocket( int retAccept )
{
	this->socket = retAccept;
	// memset( &(this->socketAddress), 0, sizeof(SockAddr) );
}

PSocket::PSocket( const PSocket& source )
{
	this->socket = source.socket;
	this->socketAddress = source.socketAddress;
}

PSocket::~PSocket()
{

}

PSocket& PSocket::operator =( const PSocket& source )
{
	this->socket = source.socket;
	this->socketAddress = source.socketAddress;

	return (*this);
}

bool PSocket::operator==( const PSocket& other )
{
	bool ret = false;

	if( 	this->socket == other.socket &&
		this->socketAddress.sin_family == other.socketAddress.sin_family &&
		this->socketAddress.sin_port == other.socketAddress.sin_port &&
		this->socketAddress.sin_addr.s_addr == const_cast<PSocket&>(other).socketAddress.sin_addr.s_addr )		
	{
		ret = true;
	}

	return ret;
}

bool PSocket::operator!=( const PSocket& other )
{
	bool ret = false;

	if( 	this->socket != other.socket ||
		this->socketAddress.sin_family != other.socketAddress.sin_family ||
		this->socketAddress.sin_port != other.socketAddress.sin_port ||
		this->socketAddress.sin_addr.s_addr != const_cast<PSocket&>(other).socketAddress.sin_addr.s_addr )		
	{
		ret = true;
	}

	return ret;
}

void PSocket::Accept( PSocket* clientSocket )
{
	int clientAddressSize = sizeof(clientSocket->socketAddress);
	*clientSocket = PSocket( accept( this->socket, (SockAddr*)&(clientSocket->socketAddress), (socklen_t*)&clientAddressSize ) );
}

void PSocket::Bind( sa_family_t sin_family, int port )
{
	this->socketAddress.sin_family = sin_family;
	this->socketAddress.sin_addr.s_addr = htonl( INADDR_ANY );
	this->socketAddress.sin_port = htons( port );
	
	bind( this->socket, (SockAddr*)(&(this->socketAddress)), sizeof(SockAddrIn) );
}

void PSocket::Close()
{
	close( this->socket );
}

void PSocket::Connect(  sa_family_t sin_family, string ipAddress, int port )
{
	this->socketAddress.sin_family = sin_family;
	this->socketAddress.sin_addr.s_addr = inet_addr( ipAddress.c_str() );
	this->socketAddress.sin_port = htons( port );
	
	connect( this->socket, (SockAddr*)(&(this->socketAddress)), sizeof(SockAddrIn) );
}


void PSocket::Listen( int backlog )
{
	listen( this->socket, backlog );
}

void PSocket::Receive()
{

}

void PSocket::Send()
{

}

/* #######################
how

0	SHUT_RD		입력스트림 종료
1	SHUT_WR		출력스트림 종료
2	SHUT_RDWR	입출력스트림 종료

####################### */

int PSocket::ShutDown( int how )
{
	return shutdown( this->socket, how );
}

int PSocket::Read( void* transferMessage, size_t size )
{
	return read( this->socket, transferMessage, size );
}

int PSocket::Write( void* transferMessage, size_t size )
{
	return write( this->socket, transferMessage, size );
}

#if 0 // TEST MAIN

int main()
{
	PSocket serverSocket( PF_INET, SOCK_STREAM, 0 );
	
	return 0;
}

#endif












